<?php
class test_TestSet extends TestSet {
  function setup() {
    $this->test = TestSet::load("samples/test_sample.php");
    $this->test->run_tests();

    ob_start();
    $this->test->print_all();
    $this->html = ob_get_contents();
    ob_end_clean();
  }

  function test_should_count_assertions() {
    $set = new TestSet();
    $set->add_assertion(new TestAssertion(false));
    $set->add_assertion(new TestAssertion(true));
    $set->add_assertion(new TestAssertion(false));
    $this->assert_equal(1, $set->pass_count);
    $this->assert_equal(3, $set->assertion_count);
  }

  function test_load_should_return_test_set_object() {
    $this->setup();
    $this->assert($this->test instanceof TestSet);
  }

  function test_should_get_the_test_class_name() {
    $this->assert($this->test->name == "test_sample");
  }

  function test_should_work_if_loading_from_here() {
    $from_here = TestSet::load("test_TestSet.php");
    $this->assert($from_here->name == "test_TestSet");
  }

  function test_should_work_with_dot_path() {
    $from_here = TestSet::load("./test_TestSet.php");
    $this->assert($from_here->name == "test_TestSet");
  }


  function test_methods_should_be_array() {
    $this->assert(is_array($this->test->methods));
  }

  function test_run_should_store_the_right_number_of_methods() {
    $this->assert(count($this->test->methods) == 2);
  }

  function test_assertions_should_be_array() {
    $this->assert(is_array($this->test->methods['test_that_fails_twice']->assertions));
  }

  function test_run_should_store_the_right_number_of_assertions() {
    $this->assert(count($this->test->methods['test_that_fails_twice']->assertions) == 2);
  }

  function test_method_should_have_line_number() {
    $this->assert($this->test->methods['test_that_passes']->line == 3);
  }

  function test_run_should_store_result_objects() {
    $this->assert($this->test->methods['test_that_passes'] instanceof TestMethod);
  }

  function test_method_should_store_assertion_objects() {
    $this->assert($this->test->methods['test_that_passes']->assertions[0] 
      instanceof TestAssertion);
  }

  function test_result_should_have_right_name() {
    $this->assert($this->test->methods['test_that_passes']->name == "test_that_passes");
  }

  function test_result_should_have_right_results() {
    $this->assert($this->test->methods['test_that_passes']->assertions[0]->passed == true);
    $this->assert($this->test->results['test_that_fails_twice']->assertions[0]->passed == false);
    $this->assert($this->test->results['test_that_fails_twice']->assertions[1]->passed == false);
  }

  function test_print_all_should_output_all() {
    $ok = ereg("test_that_passes:", $this->html);
    $this->assert($ok);
    $ok = ereg("test_that_fails_twice:", $this->html);
    $this->assert($ok);
  }

  function test_output_should_be_interlaced_with_results() {
    $this->assert(ereg("test_that_passes:.*"
      . "text printed in passing test.*"
      . "test_that_fails_twice", $this->html));
  }



  ### SUMMARY

  function passing_set() {
    $set = new TestSet();
    $set->add_assertion(new TestAssertion(true));
    $set->name = "set_name";
    return $set;
  }

  function test_output_should_include_summary() {
    $this->assert_match("3 assertions", $this->html);
    $this->assert_match("2 failures", $this->html);
  }

  function test_passing_set_table_should_be_hidden() {
    $this->assert_match("<[^>]*class=\"ResultsTable Hidden\"", 
      $this->passing_set()->toHtml());
  }

  function test_passing_set_should_have_details_link() {
    $this->assert_match("<a[^>]*href=\"javascript:toggle_div\('set_name.details'\);\"[^>]*>Show details</a>", 
      $this->passing_set()->toHtml());
  }

  function test_results_table_should_have_id() {
    $this->assert_match("id=\"set_name.details\"",
      $this->passing_set()->toHtml());
  }





  function test_whole_folder_gets_run() {
    ob_start();
    $folder = new TestFolder("samples");
    $folder->run_and_print();
    $html = ob_get_contents();
    ob_end_clean();

    $this->assert(ereg("test_empty_set", $html));
    $this->assert(ereg("test_sample", $html));
    $this->assert(ereg("test_that_passes", $html));
    $this->assert(eregi("Fail", $html));
  }
}

?>
